<template>
  <div class="playingListBg" v-show="sequenceList.length && listShow">
    <div @click="listShow = false" class="bg"></div>
    <transition name="playList-fade">
      <div class="playingList" v-show="sequenceList.length && listShow">
        <div class="head clearFloat">
          <a class="fl" @click.stop="changeMode()">
            <img :src="icoMode" alt="">
            <span>{{modeMsg}}</span>
            <span>({{sequenceList.length}})</span>
          </a>
          <a class="fr icon" @click.stop="deleteList()">
            <span class="icon clear"></span>
            <span>清空</span>
          </a>
          <a class="fr icon">
            <span class="icon collect"></span>
            <span>收藏</span>
          </a>
        </div>
        <scroll :data="sequenceList" ref="scroll" @scroll="scroll" class="list-wrapper" :listen-scroll="listenScroll"
                :probe-type="probeType">
          <ul ref="list">
            <li v-for="item, index in sequenceList" :key="item.id" class="playingList-item clearFloat"
                @click="selectItem(item, index)">
              <div class="playingList-item-left fl" :class="currentSong.id === item.id ? 'active' : ''">
                <div class="playingList-item-icon fl">
                  <img src="./voice.png" alt="" v-show="currentSong.id === item.id">
                </div>
                <div class="fl name">
                  <span>{{item.name}}</span>
                  -
                  <span v-for="singer, index in item.ar" class="singer"><span
                    v-if="index>0">/</span>{{singer.name}}</span>
                </div>
              </div>
              <div class="fr playingList-item-right" @click.stop="deleteOneSong(index)">
                <span>X</span>
              </div>
            </li>
          </ul>
        </scroll>
      </div>
    </transition>
  </div>
</template>

<script type="text/ecmascript-6">
  import Scroll from '../../base/scroll/scroll.vue'
  import {playMode} from '../../common/js/config'
  import {playModeMixin} from '../../common/js/mixin/playMode'
  import {mapActions} from 'vuex'
  export default{
    mixins: [playModeMixin],
    data() {
      return {
        scrollY: 0,
        listShow: false
      }
    },
    created() {
      this.probeType = 2
      this.listenScroll = true
    },
    methods: {
      scroll(pos) {
        this.scrollY = pos.y
      },
      show() {
        this.listShow = true
        this.$nextTick(() => {
          this.$refs.scroll.refresh()
          this.scrollToCurrent(this.currentSong)
        })
      },
      scrollToCurrent(current) {
        const index = this.sequenceList.findIndex((song) => {
          return current.id === song.id
        })
        this.$refs.scroll.scrollToElement(this.$refs.list.children[index], 150)
      },
      deleteOneSong(index) {
        this.deleteSong({
          list: this.sequenceList,
          index
        })
      },
      deleteList() {
        this.deleteSongList()
        this.listShow = false
      },
      selectItem(item, index) {
        if (this.mode === playMode.random) {
          index = this.playingList.findIndex((song) => {
            return song.id === item.id
          })
        }
        this.setCurrentIndex(index)
        this.setPlaying(true)
      },
      ...mapActions([
        'deleteSong',
        'deleteSongList'
      ])
    },
    watch: {
      scrollY(newVal) {
        if (newVal >= 50) {
          this.listShow = false
        }
      },
      currentSong(newSong, oldSong) {
        if (newSong.id === oldSong.id || !this.listShow) {
          return
        }
        this.scrollToCurrent(newSong)
      }
    },
    components: {
      Scroll
    }
  }
</script>

<style>
  .playingListBg {
    position: fixed;
    bottom: 0;
    top: 0;
    left: 0;
    right: 0;
    z-index: 1000;
    background: rgba(0, 0, 0, .5);
  }

  .playingList {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: calc(100% - 19rem);
    background: #f5f5f5;
  }

  .playingListBg .bg {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 19rem;
    z-index: 100;
  }

  .playingList a {
    height: 100%;
    color: #000;
    line-height: 4rem;
    padding: 0 .8rem;
  }

  .playingList a:active {
    background: #ccc;
  }

  .playingList .head {
    height: 4rem;
    border-bottom: 1px solid rgba(204, 204, 204, .5);
  }

  .playingList .head img {
    width: 2rem;
    vertical-align: middle;
  }

  .playingList a.icon .icon {
    display: inline-block;
    width: 16px;
    height: 14px;
    vertical-align: middle;
    background: url('./table.png') no-repeat;
  }

  .playingList a.icon .collect {
    background-position: -1px -174px;
  }

  .playingList a.icon .clear {
    background-position: -1px -217px;
  }

  .playingList-item {
    position: relative;
    height: 4rem;
    margin-left: .5rem;
    border-bottom: 1px solid rgba(204, 204, 204, .5);
  }

  .playingList-item-icon img {
    width: 1.5rem;
    margin-right: .5rem;
  }

  .playingList-item-left {
    width: 80%;
  }

  .playingList-item-left, .playingList-item-right {
    height: 100%;
    display: flex;
    align-items: center;
  }

  .playingList-item-left .name {
    text-overflow: ellipsis;
    overflow: hidden;
    white-space: nowrap;
  }

  .playingList-item-left.active, .playingList-item-left.active .singer {
    color: red;
  }

  .playingList-item-left .singer {
    font-size: 1rem;
    color: #888686;
  }

  .playingList-item-right {
    position: absolute;
    right: 0;
    padding: 0 1rem;
    color: #888686;
  }

  .list-wrapper {
    height: calc(100% - 4rem);
    overflow: hidden;
  }

  .playList-fade-enter-active, .playList-fade-leave-active {
    transition: all 0.15s linear
  }

  .playList-fade-enter, .playList-fade-leave-to {
    transform: translate3d(0, 100%, 0)
  }
</style>
